<!--
  -    Copyright (c) 2018-2025, hycloud All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: hycloud
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud ref="crud"
                 :page.sync="page"
                 :data="tableData"
                 :permission="permissionList"
                 :table-loading="tableLoading"
                 :option="tableOption"
                 @on-load="getList"
                 @search-change="searchChange"
                 @refresh-change="refreshChange"
                 @size-change="sizeChange"
                 @current-change="currentChange"
                 @row-update="handleUpdate"
                 @row-del="rowDel">
                  <template slot="menuLeft">
                    <el-button type="primary" @click="openPannel('add')"
                      >新增表格模板</el-button
                    >
                  </template>
                  <template slot-scope="{ row }" slot="menu">
                    <el-button type="text" size="small" @click.native="openPannel(row)"
                      >编辑</el-button
                    >
                  </template>
      </avue-crud>
      <el-dialog
        :visible.sync="modal"
        :fullscreen="true"
        class="gen-form-dialog"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        @close="modal = false"
      >
        <table-design
          v-if="modal"
          :showGithubStar="false"
          :toolbar="toolbar"
          @save="templateSave"
          :custom-fields="customFields"
          :import-json="editJson"
        ></table-design>
      </el-dialog>
      <el-dialog
        title="模板名称"
        width="400px"
        :visible.sync="modal2"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        class="module-dialog"
        @close="modal2 = false"
      >
        <el-input
          v-model="temParams.tableName"
          placeholder="请输入表名称"
        ></el-input>
          <el-input
          v-model="temParams.formName"
          placeholder="请输入表单名称"
        ></el-input>
        <el-select
          v-model="temParams.moduleType"
          placeholder="请选择模块类型"
        >
          <el-option
            v-for="item in dicList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <span slot="footer" class="dialog-footer">
          <el-button @click="modal2 = false">取 消</el-button>
          <el-button type="primary" @click="temSave" :loading="loading"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
  import {addObj, delObj, fetchList, putObj} from '@/api/gen/form'
  import {tableOption} from '@/const/crud/gen/form'
  import {mapGetters} from 'vuex'
  import { remote } from "@/api/admin/dict";

  export default {
    name: 'genForm',
    data() {
      return {
        customFields: [
          {
            title: '业务字段',
            list: [
              {
                title: '',
                class: 'chosemember',
                component: 'avue-input',
                placeholder: '请选择人员',
                label: '人员选择',
                span: 24,
                type: 'input',
                icon: 'icon-property',
                params: {
                  append: '选择',
                  value:''
                }
              },
            ]
          }
        ],
        searchForm: {},
        tableData: [],
        modal: false,
        modal2: false,
        loading: false,
        searchForm: {},
        temParams: {
          id: '',
          moduleType: '',
          formName: '',
          tableName: ''
        },
        dicList: [],
        jsonData: "",
        editJson: "",
        toolbar: ['import', 'generate', 'preview', 'clear'],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption: tableOption
      }
    },
    computed: {
      ...mapGetters(['permissions']),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permissions.gen_form_add, false),
          delBtn: this.vaildData(this.permissions.gen_form_del, false),
          editBtn: this.vaildData(this.permissions.gen_form_edit, false)
        };
      }
    },
    created() {
      remote('module_type').then(response => {
        this.dicList = response.data.data
      })
    },
    methods: {
      openPannel(row) {
        if (row == "add") {
          this.temParams = {
            id: '',
            moduleType: '',
            formName: '',
            tableName: ''
          }
          this.editJson = ""
        } else {
          this.temParams = row
          this.editJson = row.formInfo
        }
        this.modal = true;
      },
      templateSave(data) {
        this.modal2 = true;
        this.jsonData = data;
      },
      temSave() {
        this.loading = true;
        if (!this.temParams.tableName) {
          this.$message.error("请输入表名称");
          this.loading = false;
          return;
        }
        if (!this.temParams.formName) {
          this.$message.error("请输入表单名称");
          this.loading = false;
          return;
        }
        if (!this.temParams.moduleType) {
          this.$message.error("请选择模块类型");
          this.loading = false;
          return;
        }
        this.temParams.formInfo = this.jsonData
        if (!this.temParams.id) {
          addObj(this.temParams)
            .then((res) => {
              this.$message.success("新增模板成功");
              this.modal = false;
              this.modal2 = false;
              this.getList(this.page);
            })
            .finally(() => (this.loading = false));
        } else {
          putObj(this.temParams)
            .then((res) => {
              this.$message.success("编辑模板成功");
              this.modal = false;
              this.modal2 = false;
              this.getList(this.page);
            })
            .finally(() => (this.loading = false));
        }
      },
      getList(page, params) {
        this.tableLoading = true
        fetchList(Object.assign({
          descs: 'create_time',
          current: page.currentPage,
          size: page.pageSize
        }, params, this.searchForm)).then(response => {
          this.tableData = response.data.data.records
          this.page.total = response.data.data.total
          this.tableLoading = false
        }).catch(() => {
          this.tableLoading = false
        })
      },
      rowDel: function (row, index) {
        var _this = this
        this.$confirm('是否确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(row.id)
        }).then(data => {
          this.$message.success('删除成功')
          this.getList(this.page)
        })
      },
      handleUpdate: function (row, index, done, loading) {
        putObj(row).then(data => {
          this.$message.success('修改成功')
          done()
          this.getList(this.page)
        }).catch(() => {
          loading();
        });
      },
      searchChange(form, done) {
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize
      },
      currentChange(current){
        this.page.currentPage = current
      },
      refreshChange() {
        this.getList(this.page)
      }
    }
  }
</script>

<style lang="scss">
.module-dialog{
  .el-input {
    margin-bottom: 10px;
  }
  .el-select{
    width: 100%;
  }
}
.gen-form-dialog{
  .form-designer {
    height: 800px;
  }
}
</style>
